package org.nnxy.foodboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.nnxy.foodboot.entity.UserFavorite;
import org.nnxy.foodboot.mapper.UserFavoriteMapper;
import org.nnxy.foodboot.service.UserFavoriteService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用户收藏服务实现类
 */
@Service
@RequiredArgsConstructor
public class UserFavoriteServiceImpl extends ServiceImpl<UserFavoriteMapper, UserFavorite> implements UserFavoriteService {

    private final UserFavoriteMapper userFavoriteMapper;

    @Override
    public boolean addUserFavorite(Integer userId, Integer recipeId, String note) {
        if (userId == null || recipeId == null) {
            return false;
        }
        
        // 检查是否已经收藏
        if (isRecipeFavorited(userId, recipeId)) {
            return true; // 已经收藏过了，视为添加成功
        }
        
        // 创建新的收藏记录
        UserFavorite favorite = new UserFavorite();
        favorite.setUserId(userId);
        favorite.setRecipeId(recipeId);
        favorite.setCreatedTime(LocalDateTime.now());
        favorite.setNotes(note);
        
        return save(favorite);
    }

    @Override
    public boolean removeUserFavorite(Integer userId, Integer recipeId) {
        if (userId == null || recipeId == null) {
            return false;
        }
        
        LambdaQueryWrapper<UserFavorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFavorite::getUserId, userId)
                .eq(UserFavorite::getRecipeId, recipeId);
        
        return remove(queryWrapper);
    }

    @Override
    public List<UserFavorite> getUserFavoritesByUserId(Integer userId) {
        if (userId == null) {
            return List.of();
        }
        
        LambdaQueryWrapper<UserFavorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFavorite::getUserId, userId)
                .orderByDesc(UserFavorite::getCreatedTime);
        
        return list(queryWrapper);
    }

    @Override
    public Page<UserFavorite> getUserFavoritesPage(Page<UserFavorite> page, Integer userId) {
        if (userId == null) {
            return new Page<>();
        }
        
        LambdaQueryWrapper<UserFavorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFavorite::getUserId, userId)
                .orderByDesc(UserFavorite::getCreatedTime);
        
        return page(page, queryWrapper);
    }

    @Override
    public boolean isRecipeFavorited(Integer userId, Integer recipeId) {
        if (userId == null || recipeId == null) {
            return false;
        }
        
        LambdaQueryWrapper<UserFavorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFavorite::getUserId, userId)
                .eq(UserFavorite::getRecipeId, recipeId);
        
        return count(queryWrapper) > 0;
    }

    @Override
    public boolean updateFavoriteNote(Integer id, String note) {
        if (id == null) {
            return false;
        }
        
        LambdaUpdateWrapper<UserFavorite> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserFavorite::getId, id)
                .set(UserFavorite::getNotes, note);
        
        return update(updateWrapper);
    }

    @Override
    public long countByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        if (startTime == null || endTime == null) {
            return 0;
        }
        
        LambdaQueryWrapper<UserFavorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(UserFavorite::getCreatedTime, startTime, endTime);
        
        return count(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> getMostFavoritedRecipes(Integer limit) {
        if (limit == null || limit <= 0) {
            limit = 10;
        }
        
        // 此处应该使用自定义SQL来获取最多收藏的食谱
        // 由于复杂SQL需要在Mapper中定义，这里简化处理
        return userFavoriteMapper.getMostFavoritedRecipes(limit);
    }
} 